/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.gadgets.docsFolder.client;



import com.gadglet.client.gwt.GadgetNativeUtils;
import com.gadglet.client.gwt.google.DocsFolderItem;
import com.gadglet.client.gwt.google.GoogleRequest;
import com.gadglet.client.gwt.ui.HomeViewMainPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.PreferencesFeature;
import com.google.gwt.gadgets.client.PreferencesProvider;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FoldersTree extends GoogleRequest{
	
	final VerticalPanel holder = new VerticalPanel();

	
	final PreferencesFeature  prefs = PreferencesProvider.get();
	Button selectFolder;
	
	FoldersTree(HomeViewMainPanel mainPanel)
	{
		super();
	    mainPanel.add(holder);
		 selectFolder = new Button (prefs.getMsg("gadgetLabelselectFolder"));
		holder.add(selectFolder);
		selectFolder.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				getFolders();	
			}
		});
		holder.setVisible(false);
		
		
	}
	
	void init(){
		holder.setVisible(true);
	GadgetNativeUtils.setTitle(prefs.getMsg("gadgetLabelselectFolder"));
	}
	
	
	void getFolders()
	{
		// folder list not ;
		if(itemsList.size() == 0){
			String feedUrl = "https://docs.google.com/feeds/documents/private/full/-/folder?alt=json";
			setFeedUrl(feedUrl);
			setHeaders("GData-Version","2.0");
			setAuthorizationMsg("gadgetMsgPleaseApproveDocs");
			callFeed();	
		}
	
		
	}
	
	@Override
	protected void  processResults(){
		
		selectFolder.setVisible(false);
		DocsFolderItem docsFolderItem = null;
		TreeItem treeItem  = null;
		Tree root = new Tree();
		TreeLabel itemLabel = null;
			
	    for (int y =0; y< itemsList.size(); y++){
	    	  
	    	docsFolderItem = new DocsFolderItem(itemsList.get(y));
	    	  
	    	 // find root level foders
	    	  if (!docsFolderItem.hasParent()){
	    		 
	    		  itemLabel = new TreeLabel(docsFolderItem.getID(),docsFolderItem.getTitle());
	    		  
	    		  //itemLabel.addStyleName("treeItemSelect");
	    		  itemLabel.addClickHandler(new ClickHandler() {
	    				@Override
						public void onClick(ClickEvent event) {
	    					// set the User pref 
	    					String id = 	((TreeLabel)event.getSource()).getID();
	    					prefs.set("selectedFolder", id);
	    					String title =  ((TreeLabel)event.getSource()).getText();
	    					prefs.set("selectedFolderTitle", title);
	    					
	    					holder.setVisible(false) ;
	    						    					
	    					DocsFolderGadget.DL.init();
	    				}
	    			});
	    		    		  
	    		  treeItem = new TreeItem(itemLabel);
	    		  root.addItem(treeItem); 
		    	  buildSubFoders( docsFolderItem, treeItem);
	    	  }
	    }
	   
	    holder.add(root);
	  
	  
	}

	void buildSubFoders(DocsFolderItem currentItem, TreeItem currentTreeItem){
		DocsFolderItem subFolderItem = null;
		TreeItem subTreeItem  = null;
		TreeLabel itemLabel = null;
		for (int y =0; y< itemsList.size(); y++){
			subFolderItem = new DocsFolderItem(itemsList.get(y));
					
			// check for siblings 
			if(currentItem.getID().equals(subFolderItem.getParentID()))
			{
				itemLabel = new TreeLabel(subFolderItem.getID(),subFolderItem.getTitle());
				// itemLabel.addStyleName("treeItemSelect");
				itemLabel.addClickHandler(new ClickHandler() {
	    				@Override
						public void onClick(ClickEvent event) {
	    					// set the User pref 
	    					String id = 	((TreeLabel)event.getSource()).getID();
	    					String title =  ((TreeLabel)event.getSource()).getText();
	    					prefs.set("selectedFolderTitle", title);
	    					prefs.set("selectedFolder", id);
	    					// set not visible to
	    					
	    					holder.setVisible(false) ;
	    					DocsFolderGadget.DL.init();
	    				}
	    			});
				subTreeItem = new TreeItem(itemLabel);
	    		currentTreeItem.addItem(subTreeItem); 
	    		buildSubFoders( subFolderItem, subTreeItem);
			}
	    	
	    }
	}
	
	protected class TreeLabel extends Label{
		public String getID() {
			return ID;
		}
		String ID;
		TreeLabel(String ID, String title){
			super(title);
			this.ID = ID;
		}
	}
	

}
